package jee.boot.common.service;

import jee.boot.common.basic.CrudService;
import jee.boot.common.dao.SerialNumberFormatDao;
import jee.boot.entity.system.SerialNumberFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
/**
 * SerialNumber格式
 * 目前支持 前缀+日期+自增序号
 */
@Service
public class SerialNumberFormatService extends CrudService<SerialNumberFormatDao, SerialNumberFormat> {
    @Autowired
    private SerialNumberHandler serialNumberHandler;
    @Cacheable(value = "serialNumberFormat", key = "#entity.entityKey")
    @Override
    public SerialNumberFormat get(SerialNumberFormat entity) {
        return super.get(entity);
    }
    @Caching(evict = {
            @CacheEvict(value = "serialNumberFormat", key = "#entity.entityKey"),
    })
    @Override
    public void update(SerialNumberFormat entity) {
        super.update(entity);
    }
    @Caching(evict = {
            @CacheEvict(value = "serialNumberFormat", key = "#entity.entityKey"),
    })
    @Override
    public void delete(SerialNumberFormat entity) {
        super.delete(entity);
    }

    public void test(SerialNumberFormat entity) {
        Thread t1=new Thread(new Sn(serialNumberHandler),"a");
        Thread t2=new Thread(new Sn(serialNumberHandler),"b");
        Thread t3=new Thread(new Sn(serialNumberHandler),"c");
        t1.start();
        t2.start();
        t3.start();
    }

}
class Sn implements Runnable{
    private SerialNumberHandler serialNumberHandler;

    public Sn(SerialNumberHandler serialNumberHandler) {
        this.serialNumberHandler = serialNumberHandler;
    }

    @Override
    public void run() {
        int a=100;
        for(int i=0;i<a;i++) {
            SerialNumberFormat snf = SerialNumberHandler.DEFAULT_SERIAL_NUMBER_FORMAT_DAY;
            String system = serialNumberHandler.getSerialNumber("system", "user-serial", snf);
            System.out.println(Thread.currentThread().getName()+"->"+system);
        }
    }
}
